Add directory redirection handling to cowboy_static #1588
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Directory accesses with no trailing slash will by default be redirected (301) to their canonical slashed version. New handler option 'directory_index' can be set to 'false' to disable, or to another status code such as 302.
Directory accesses with a trailing slash can be redirected (301) to an index file. This is off by default. New handler option 'directory_index' can be set to 'true' or a status code such as 302, for the default "index.html", or to a binary string such as <<"index.php">>, or to a pair {Code, Filename}.
Motivation: I think cowboy_static is the correct place for handling this. In principle, it's not a confirmed directory access until the request has been handled to cowboy_static, and it has checked what the file path points to. Furthermore, the feature ought to work also for archives. Hence, doing this in a middleware module (which I did at first to verify my approach) duplicates effort, and runs the risk of doing something different from what cowboy_static does, or perform a redirection on a path that should not be handled as a static file.
I also feel that these features are a minimal requirement for using cowboy to serve a static web site, and that cowboy should handle it out of the box rather than requiring a user to learn how to implement middleware modules. The code is straightforward and does not complicate the module much.
Example route for a static site:
See also #1346.